package com.egg.auth.controller.feign;

import com.egg.auth.entity.user.UserAccount;
import com.egg.auth.enums.account.AccountTypeEnum;
import com.egg.auth.feign.AuthAccountFeignClient;
import com.egg.auth.model.response.web.UserAccountInfo;
import com.egg.auth.service.user.IUserAccountService;
import com.egg.common.core.enums.ResponseCode;
import com.egg.common.core.model.controller.FeignController;
import com.egg.common.core.model.response.ResponseData;
import com.egg.common.util.JsonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

/**
 * @author east
 */
@Slf4j
@Api(tags = FeignController.API_PREFIX + "账号中心")
@RestController
@RequestMapping("/account")
public class FeignAccountController extends FeignController implements AuthAccountFeignClient {


    @Autowired
    private IUserAccountService userAccountService;


    @Override
    @ApiOperation(value = "账号信息")
    @GetMapping("/info")
    public ResponseData<UserAccountInfo> userAccountInfo(
            @ApiParam(value = "用户ID", required = true) @RequestParam(value = "userId") Long userId
            , @ApiParam(value = "授权类型", required = true) @RequestParam(value = "accountType") String accountType
    ) {
        Optional<AccountTypeEnum> accountGrantType = AccountTypeEnum.find(accountType);
        if (!accountGrantType.isPresent()) {
            return ResponseData.error(ResponseCode.PARAM_INVALID);
        }
        Optional<UserAccount> userAccount = userAccountService.findByUserId(userId, accountGrantType.get());
        if (!userAccount.isPresent()) {
            return ResponseData.error(ResponseCode.PARAM_INVALID);
        }
        UserAccountInfo userAccountInfo = JsonUtil.objToObj(userAccount.get(), UserAccountInfo.class);
        return ResponseData.success(userAccountInfo);
    }


}
